﻿using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using NUnit.Framework;

namespace com.ptrampert.LibVLCBind.Tests
{
    /// <summary>
    /// Summary description for TestVLCMedia
    /// </summary>
    [TestFixture]
    public class TestMedia
    {
        public TestMedia()
        {
        }

        private const string media = "10 Spontaneous Me.mp3";
        private const string mediaCopy = "copy.mp3";
        private TestContext testContextInstance;
        private IVLCInstance vlc;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestFixtureSetUp]
        public void MyClassInitialize()
        {
            IVLCFactory factory = new TwoflowerVLCFactory();
            vlc = factory.InitializeVLC();
            if (File.Exists(mediaCopy))
            {
                File.Delete(mediaCopy);
            }
        }

        [TestFixtureTearDown]
        public void MyClassCleanup()
        {
            vlc.Dispose();
            if (File.Exists(mediaCopy))
            {
                File.Delete(mediaCopy);
            }
        }

        [Test]
        public void TestParse()
        {
            using (IMedia m = vlc.GetVLCMedia(media, true))
            {
                m.Parse();
                Assert.IsTrue(m.IsParsed);
                Assert.AreEqual("Lindsey Stirling", m.Artist);
                Assert.AreEqual("Spontaneous Me", m.Title);
            }
        }

        [Test]
        public void TestSaveChanges()
        {
            File.Copy(media, mediaCopy);
            using (IMedia m = vlc.GetVLCMedia(mediaCopy, true))
            {
                m.Parse();
                m.Title = "test";
                m.SaveChanges();
            }
            using (IMedia m = vlc.GetVLCMedia(mediaCopy, true))
            {
                m.Parse();
                Assert.AreEqual("test", m.Title);
            }
        }

        [Test]
        public void TestStats()
        {
            using (IMediaPlayer mp = vlc.CreateMediaPlayer())
            {
                using (IMedia m = vlc.GetVLCMedia(media, true))
                {
                    Assert.IsNull(m.Stats);
                    mp.Media = m;
                }
                mp.Play();
                MediaStats stats = mp.Media.Stats;
                Assert.IsNotNull(stats);
            }
        }

        [Test]
        public void TestClone()
        {
            using (IMedia m = vlc.GetVLCMedia(media, true))
            {
                using (IMedia m2 = (IMedia)m.Clone())
                {
                    Assert.IsFalse(m == m2);
                    m.Parse();
                    m2.Parse();
                    Assert.AreEqual(m.Title, m2.Title);
                }
            }
        }

        [Test]
        public void TestMediaAsync()
        {
            using (IMedia m = vlc.GetVLCMedia(media, true))
            {
                bool marshaledCorrectly = false;
                m.IsParsedChanged += (object o, BoolEventArgs args) =>
                    {
                        marshaledCorrectly = args.Value;
                    };
                m.Parse();
                Thread.Sleep(1000);
                Assert.IsTrue(marshaledCorrectly);
            }
        }
    }
}
